package com.wanma.thread;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;


public class SendMail {

	public static void main(String[] args) {
		// 1.初始化用户数据
		List<Book> books = init();
		// 2.计算创建创建多少个线程并且每一个线程需要执行“分批发送短信用户”
		List<Spliterator<Book>> spliterators = new ArrayList<Spliterator<Book>>();
		 Spliterator<Book> book1 = books.spliterator();
		 Spliterator<Book> book2 = book1.trySplit();
		 Spliterator<Book> book3 = book1.trySplit();
		 Spliterator<Book> book4 = book2.trySplit();
		 spliterators.add(book1);
		 spliterators.add(book2);
		 spliterators.add(book3);
		 spliterators.add(book4);
		// 每一个线程分批跑多少
		 for (int i = 0; i < spliterators.size(); i++) {
			 MySpliterator mySpliterator = new MySpliterator(spliterators.get(i));
			 mySpliterator.start();
		}
	}

	private static List<Book> init() {
		List<Book> books = new ArrayList<Book>();
		for(int i=0; i < 520 ;i++){
			Book book = new Book("520czh"+i, "hxy"+i);
			books.add(book);
		}
		return books;
	}
}
class MySpliterator extends Thread{
	private Spliterator<Book> list;
	public MySpliterator(Spliterator<Book> list){
		this.list = list;
	}
	@Override
	public void run() {
		Stream<Book> parallelStream = StreamSupport.stream(list, true);
		parallelStream.distinct().forEach(i -> System.out.print(i.getName() + "/t"));
		System.out.println(Thread.currentThread().getName());
	}
}

